package org.lq.dlz.web;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.lq.dlz.common.Page;
import org.lq.dlz.dao.CategoryDao;
import org.lq.dlz.dao.impl.CategoryDaoImpl;
import org.lq.dlz.entity.Category;
import org.lq.dlz.service.CategoryService;
import org.lq.dlz.service.impl.CategoryServiceImpl;

import com.google.gson.Gson;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet("/finalCategoryServlet.do")
public class finalCategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public finalCategoryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    private CategoryService categoryservice = new CategoryServiceImpl();
    private CategoryDao categoryDao = new CategoryDaoImpl();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(StringUtils.isNotBlank(method)) {
			switch (method) {
			case "initParent":
				initParent(request,response);
				break;
			case "addCate":
				addCate(request,response);
				break;
			case "findAllCate":
				findAllCate(request,response);
				break;
			case "findChildByPid":
				findChildByPid(request,response);
				break;
			case "delCate":
				delCate(request,response);
				break;
			case "preupdate":
				preupdate(request,response);
				break;
			case "updateCate":
				updateCate(request,response);
				break;
			default:
				break;
			}
		}
		
	}

	private void updateCate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = request.getParameter("dataN");
		System.out.println(data);
		
		Category fromJson = new Gson().fromJson(data, Category.class);
		fromJson.setUpdate_time(new Date());
		System.out.println(fromJson);
		boolean updateCategory = categoryservice.UpdateCategory(fromJson);
		if(updateCategory) {
			response.getWriter().write("ok");
		}
	}

	private void preupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String data = request.getParameter("data");
		System.out.println(data);
		Category category = categoryservice.getCategoryById(Integer.valueOf(data));
		request.setAttribute("category",category);
		System.out.println("------");
		System.out.println(category);
		request.getRequestDispatcher("final/addProductCate.jsp").forward(request, response);
	}

	private void delCate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//判断该分类下面是否有商品，没有就可以删除，有就不可以删除
		int cid = Integer.valueOf(request.getParameter("cid"));
		if(categoryservice.deleteCategoryById(cid)>=0) {
			response.getWriter().write("no");
		}else {
			response.getWriter().write("ok");
		}
	}

	private void findChildByPid(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int parentid = Integer.valueOf(request.getParameter("parentid"));
		String pageStart = request.getParameter("page");
		String pageSize = request.getParameter("limit");
		//根据父id获取下面的分类
		System.out.println(parentid+"-----------");
		Page<Category> page = new Page<>(Integer.valueOf(pageStart),Integer.valueOf(pageSize),categoryDao.findByParent().size());
		List<Category> parent = categoryDao.findByParentId(parentid,page);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", parent.size());
		map.put("data",parent);
		response.getWriter().write(new Gson().toJson(map));
	}

	private void findAllCate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Category> parent = categoryservice.findparent();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", parent.size());
		map.put("data",parent);
		response.getWriter().write(new Gson().toJson(map));
	}

	private void addCate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = request.getParameter("dataN");
		System.out.println(data);
		
		Category fromJson = new Gson().fromJson(data, Category.class);
		System.out.println(fromJson);
		boolean save = categoryservice.saveCategory(fromJson);
		if(save) {
			response.getWriter().write("ok");
		}
	}

	private void initParent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Category> parent = categoryservice.findparent();
		System.out.println(parent.size());
		response.getWriter().write(new Gson().toJson(parent));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
